package com.skyjing.askit.java.temp;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

import com.skyjing.askit.AsTool;


/**
 * 弹出对话框，询问是否打开GPS
 * @author Administrator
 */
public class LocationSwitch {

    private LocationSwitch() {
    }

    public static LocationSwitch get() {
        return new LocationSwitch();
    }

    public void showDialogAndResult(int tipsStringId, int okBtnStringId, int cancelStringId, ControlGpsListener listener) {
        if (AsTool.getApp() == null) {
            throw new IllegalArgumentException("the context can not be null");
        }
        creatDialog(AsTool.getApp().getResources().getString(tipsStringId),
                AsTool.getApp().getResources().getString(okBtnStringId),
                AsTool.getApp().getResources().getString(cancelStringId),
                listener);
    }

    public void showDialogAndResult(String tipsString, String okBtnString, String cancelString, ControlGpsListener listener) {
        if (AsTool.getApp() == null) {
            throw new IllegalArgumentException("the context can not be null");
        }
        creatDialog(tipsString, okBtnString, cancelString, listener);
    }

    private void creatDialog(String tipsString, String okBtnString, String cancelString, final ControlGpsListener listener) {
        LocationManager locationManager = (LocationManager) AsTool.getApp().getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(AsTool.getApp());
                builder.setMessage(tipsString);
                builder.setPositiveButton(okBtnString,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    //                            Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                                // startActivityForResult(intent, 0); // 设置完成后返回到原来的界面
                                if (listener != null) {
                                    listener.ok(intent);
                                }
                            }
                        });
                builder.setNegativeButton(cancelString,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // TODO Auto-generated method stub
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            } else {
                if (listener != null) {
                    listener.isOpened();
                }
            }
        }
    }

    public interface ControlGpsListener {
        void isOpened();

        void ok(Intent intent);
//        void cancle(Intent intent);
    }
}
